$(document).ready(function () {
    var ctx = $("#ctx").val();//获取服务器地址
    // 全选
    $('#selectOrderAll').click(function () {
        $(".input-radiosDiv input[type='checkbox']").attr("checked", true);
        return false;
    });

    //保存
    $('#order_btn_save').click(function () {
        var checkboxs = $("#selectOrderForm input[type='checkbox']:checked");
        var adgroupIds = $("#adgroupIds").val();
        var orderIds = '';
        if (checkboxs.length <= 0) {
            bootbox.alert("请选择订单");
            return false;
        }
        orderIds = $(':checked').map(function () {
            return this.value;
        }).get().join(',');
        bootbox.confirm("你将要把ID为【" + adgroupIds + "】的策略复制到ID为【" + orderIds + "】的订单下，确定复制吗?", function (ensure) {
            if (ensure) {
                $.ajax({
                    async: true,
                    type: "POST",
                    url: 'copyAdgroup2Order',
                    data: {
                        "adgroupIds": adgroupIds,
                        "orderIds": orderIds
                    },
                    success: function (data) {
                        if (data.status == "OK") {
                            bootbox.confirm(data.msg + ",是否关闭弹窗？", function (flag) {
                                if (flag) {
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index);//再执行关闭
                                }
                            });
                        } else if (data.status == "ERROR") {
                            boot.alert(data.msg);
                        }
                    }
                });
            } else {
                bootbox.alert("你取消了复制");
            }
        });
        return false;
    });

    //关闭
    $('#order_btn_close').click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);//再执行关闭
    });

    //复制投放按钮
    $("#btn_copy_adgroup").click(function () {
        var checkboxs = $("table tbody .checkboxStyle_change");
        var advertiserId = $("#advertiserId").val();
        var ids = [];
        if (checkboxs.length > 0) {
            checkboxs.each(function () {
                var id = parseInt($(this).attr("id"));
                ids.push(id);
            });
            layer.open({
                type: 2,
                title: '请选择订单',
                shadeClose: false,
                shade: 0.6,
                minmax: true,
                fix: false,
                closeBtn: 1,
                area: ['800px', '500px'],
                content: ctx + '/advertiser/main/ad/target/ad-group-alloperator/toorderWindow?adgroupIds=' + ids + "&advertiserId=" + advertiserId
            });
        } else {
            bootbox.alert("请选择要复制的策略！");
            return false;
        }
    });

});